
cmake_minimum_required(VERSION 2.6)

set (TARGET_NAME "cl-qsvdec")

set( EXTRA_FILES src/kernels/vector_add_kernel.cl )

project(cl_media)

# Find OpenCV components if exist
find_package(OpenCV COMPONENTS highgui QUIET)
if(NOT(OpenCV_FOUND))
    message(WARNING "OPENCV is disabled or not found, " ${TARGET_NAME} " skipped")
    return()
endif()

set( FFMPEG_INCLUDES "C:/temp_20151027/ffmpeg-4.3.1-2020-11-08-full_build-shared/include" )

set( FFMPEG_LIBRARY_PATHS "C:/temp_20151027/ffmpeg-4.3.1-2020-11-08-full_build-shared/lib" )
set( FFMPEG_LIBRARYS ${FFMPEG_LIBRARY_PATHS}/avcodec.lib ${FFMPEG_LIBRARY_PATHS}/avformat.lib  ${FFMPEG_LIBRARY_PATHS}/avutil.lib ${FFMPEG_LIBRARY_PATHS}/swscale.lib)

#message(STATUS "FFMPEG_LIB_PATH: ${FFMPEG_LIBRARYS_PATHS}")

#find_library( FFMPEG_LIBS
#NAMES avcodec avformat avutil swscale
#  PATHS
#    ${FFMPEG_LIBRARYS_PATHS}
#)

if (NOT CMAKE_BUILD_TYPE)
  message(STATUS "Setting build type to Release")
  set(CMAKE_BUILD_TYPE "Release")
endif()

# Determine machine bitness
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
  set(BITNESS 64)
else()
  set(BITNESS 32)
endif()

# Find OpenCL include directories
find_path( OPENCL_INCLUDES
  NAMES CL/cl.h OpenCL/cl.h
  HINTS
    $ENV{AMDAPPSDKROOT}/include
    $ENV{INTELOCLSDKROOT}/include
    $ENV{CUDA_PATH}/include
    $ENV{OPENCL_ROOT}/include
  PATHS
    /usr/include
    /usr/local/include
  )

# Find OpenCL libraries
if(BITNESS EQUAL 64)
  find_library( OPENCL_LIBS
    NAMES OpenCL
    HINTS
      $ENV{AMDAPPSDKROOT}/lib
      $ENV{INTELOCLSDKROOT}/lib
      $ENV{CUDA_PATH}/lib
      $ENV{OPENCL_ROOT}/lib
    PATH_SUFFIXES x86_64 x64
    PATHS
      /usr/lib64
      /usr/lib
      /usr/local/lib
  )
elseif(BITNESS EQUAL 32)
  find_library( OPENCL_LIBS
    NAMES OpenCL
    HINTS
      $ENV{AMDAPPSDKROOT}/lib
      $ENV{INTELOCLSDKROOT}/lib
      $ENV{CUDA_PATH}/lib
      $ENV{OPENCL_ROOT}/lib
    PATH_SUFFIXES x86 Win32
    PATHS
      /usr/lib32
      /usr/lib
      /usr/local/lib
  )
endif()

if( (NOT OPENCL_INCLUDES) OR (NOT OPENCL_LIBS) )
  message( FATAL_ERROR "Could not find OpenCL include/libs. Set OPENCL_ROOT to your OpenCL SDK. Download AMD APP SDK "
      "http://developer.amd.com/tools-and-sdks/heterogeneous-computing/amd-accelerated-parallel-processing-app-sdk/ for x86/x64 "
      "or pocl http://pocl.sourceforge.net/ for ARM systems" )
else()
  message(STATUS "Selected OpenCL includes from ${OPENCL_INCLUDES}")
  message(STATUS "Selected OpenCL lib ${OPENCL_LIBS}")
endif()

if(CMAKE_CXX_COMPILER MATCHES ".*clang")
  set(CMAKE_COMPILER_IS_CLANGXX 1)
endif()

if(APPLE)
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
endif()

if(CMAKE_COMPILER_IS_GNUCXX  OR CMAKE_COMPILER_IS_CLANGXX)
  execute_process(COMMAND ${CMAKE_C_COMPILER} -dumpversion OUTPUT_VARIABLE GCC_VERSION)
  if(GCC_VERSION VERSION_GREATER 4.7 OR GCC_VERSION VERSION_EQUAL 4.7 OR CMAKE_COMPILER_IS_CLANGXX)
    add_definitions("-std=gnu++11")
  elseif(GCC_VERSION VERSION_GREATER 4.3 OR GCC_VERSION VERSION_EQUAL 4.3)
    add_definitions("-std=gnu++0x")
  else()
    message(FATAL_ERROR "C++11 needed. Therefore a gcc compiler with a version higher than 4.3 is needed.")
  endif()

  add_definitions("-fPIC -Wall -Wextra -Wno-deprecated-declarations -Wno-unused-parameter -Wno-ignored-attributes")
endif()

# override cl.hp from deps. Its buggy or not present in come systems
include_directories("deps/OpenCL-CLHPP/include/")

include_directories(${OPENCL_INCLUDES} "include" "src/kernels")

# directory of opencv headers
include_directories(${OpenCV_INCLUDE_DIRS} ${FFMPEG_INCLUDES})

set(SOURCE_FILES
  src/main.cpp
  )


# directory of opencv library
link_directories(${OpenCV_LIBRARY_DIRS} ${FFMPEG_LIBRARY_PATHS})

add_executable(${TARGET_NAME} ${SOURCE_FILES} ${EXTRA_FILES})


target_link_libraries(${TARGET_NAME} ${OPENCL_LIBS} ${OpenCV_LIBRARIES} ${FFMPEG_LIBRARYS})


set_target_properties(${TARGET_NAME} PROPERTIES 
    #VS_DEBUGGER_COMMAND "debug_command"
    VS_DEBUGGER_COMMAND_ARGUMENTS "0.mp4 1.mp4")

#SET(CMAKE_DEFAULT_STARTUP_PROJECT ${TARGET_NAME})
set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT ${TARGET_NAME})

# Copy extra files to binary directory
foreach( extra_file ${EXTRA_FILES} )
   add_custom_command(
       TARGET ${TARGET_NAME} POST_BUILD
#       COMMAND ${CMAKE_COMMAND} -E copy_if_different
#       ${CMAKE_CURRENT_SOURCE_DIR}/${extra_file}  $(OutputPath)

       COMMAND ${CMAKE_COMMAND} -E copy_if_different
       ${CMAKE_CURRENT_SOURCE_DIR}/${extra_file}  ${PROJECT_BINARY_DIR}

       COMMAND ${CMAKE_COMMAND} -E copy_if_different
       ${CMAKE_CURRENT_SOURCE_DIR}/${extra_file}   $<TARGET_FILE_DIR:${TARGET_NAME}>
       )
endforeach( extra_file )

# message("ISPC_FLAGS 111  = ${IMPORTED_LOCATION_RELEASE}")

